﻿using System;
using System.Collections.Generic;
using Apache.Cassandra;
using Thrift.Protocol;
using Thrift.Transport;
using System.Linq;

namespace LarasMutiaraDiva.Skripsi.Libs
{
    static class TriftUtility
    {
        private static void Main2(string[] args)
        {
            Console.WriteLine(GetDocLength("deptan14042010.txt"));
            Console.ReadLine();
        }
        
        
        public static Dictionary<string, List<string >> GetAllTerms()
        {
            TTransport transport = new TBufferedTransport(new TSocket(CassandraApp.cassandraHost, CassandraApp.cassandraPort));
            TProtocol protocol = new TBinaryProtocol(transport);
            Cassandra.Client client = new Cassandra.Client(protocol);
            //client.
            //Console.WriteLine("Opening connection");
            transport.Open();

            System.Text.Encoding utf8Encoding = System.Text.Encoding.UTF8;
           
            //Read an entire row
            SlicePredicate predicate = new SlicePredicate()
            {
                Slice_range = new SliceRange()
                {
                    //Start and Finish cannot be null
                    Start = new byte[0],
                    Finish = new byte[0],
                    Count = 99999999,
                    Reversed = true
                }
            };
            
            var keyRange = new KeyRange();
            keyRange.Start_key = "";
            keyRange.End_key = "";
            keyRange.Count = 99999999;//
            

            var all = client.get_range_slices(
                       CassandraApp.cassandraKeyspace,
                       new ColumnParent("Documents"),
                       predicate,
                       keyRange, 
                       ConsistencyLevel.ALL
                );

            //int i = 0;

             
            var results = new Dictionary<string, List<string>>();
            foreach (var keySlice in all)
            {
                    var supercolumns = new List<string>();
                    var supcolumns = keySlice.Columns;
                    foreach (var supcolumn in supcolumns)
                    {                        
                        supercolumns.Add(supcolumn.Super_column.Name.DecodeAsciiString());
                    }
                    results.Add(keySlice.Key, supercolumns);
                    //i++;
            }

            //client.
            transport.Close();

            return results;
        }

        public static double GetDocLength(string docName)
        {
            TTransport transport = new TBufferedTransport(new TSocket(CassandraApp.cassandraHost, CassandraApp.cassandraPort));
            TProtocol protocol = new TBinaryProtocol(transport);
            Cassandra.Client client = new Cassandra.Client(protocol);
            //client.
            //Console.WriteLine("Opening connection");
            transport.Open();

            System.Text.Encoding utf8Encoding = System.Text.Encoding.UTF8;

            //Read an entire row
            SlicePredicate predicate = new SlicePredicate()
            {
                Slice_range = new SliceRange()
                {
                    //Start and Finish cannot be null
                    Start = new byte[0],
                    Finish = new byte[0],
                    Count = 99999999,
                    Reversed = true
                }
            };

            var keyRange = new KeyRange();
            keyRange.Start_key = "";
            keyRange.End_key = "";
            keyRange.Count = 99999999;//


            var all = client.get_range_slices(
                       CassandraApp.cassandraKeyspace,
                       new ColumnParent("Documents"),
                       predicate,
                       keyRange,
                       ConsistencyLevel.ALL
                );


           // Console.WriteLine(all.Count);
            double totalTfIdf = 0;
            foreach (var keySlice in all)
            {
                
                var supercolumns = keySlice.Columns;

                foreach (var columnOrSuperColumn in supercolumns)
                {
                    if (columnOrSuperColumn.Super_column.Name.DecodeAsciiString().Equals(docName))
                    {
                        
                        var columns = columnOrSuperColumn.Super_column.Columns;
                        var tf_idf = columns.ToList()[1].Value.DecodeAsciiString();
                        totalTfIdf += Math.Pow(Convert.ToDouble(tf_idf), 2);
                       // Console.WriteLine("['{0}']['{1}'] => {2}", keySlice.Key, docName, tf_idf);
                    }
                }
               
            }

            transport.Close();
            return Math.Sqrt(totalTfIdf);
        }
    }
}
